package com.example.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import com.example.springboot.common.Result;
import com.example.springboot.controller.request.CollectionsTypePageRequest;
import com.example.springboot.entity.CollectionsType;
import com.example.springboot.service.ICollectionsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin  //解决跨域错误
@RestController
@RequestMapping("/collectionstype")
//是rest风格的collectionstypecontroller
public class CollectionsTypeController {

    @Autowired
    ICollectionsTypeService collectionstypeService;


    //    CollectionsTypePageRequest传入用户名和电话信息
    @GetMapping("/page")
    public Result page(CollectionsTypePageRequest collectionstypePageRequest){
        //        返回数据 Result.success传入一个对象
        return Result.success(collectionstypeService.page(collectionstypePageRequest));
    }

//    post put get delete对应增改查删四类操作
    //    新增会员
    @PostMapping("/save")
    public Result save(@RequestBody CollectionsType collectionstype){
        collectionstypeService.save(collectionstype);
        return Result.success();
    }

//    更新会员信息
    @PutMapping("/update")
    public Result update(@RequestBody CollectionsType collectionstype){
        collectionstypeService.update(collectionstype);
        return Result.success();
    }

    //    获取用户列表
    @GetMapping("/list")
    public Result list(){
        //        返回数据
        List<CollectionsType> list=collectionstypeService.list();
//        System.out.println("list内容是"+list);
        return Result.success(list);

    }

    @GetMapping("/tree")
    public Result tree(){
        List<CollectionsType> list=collectionstypeService.list();
        //        对list操作即可  第一层
        List<CollectionsType> treeList = list.stream().filter(v -> v.getPid() == null).collect(Collectors.toList());
        //比如 递归实现 children列表的查询
        System.out.println("list内容是"+list);
        System.out.println("treeList内容是"+treeList.toString());
        return Result.success(createTree(null,list));//null 表示从第一级开始递归
    }

    //完全递归的方法来实现递归树
    private List<CollectionsType> createTree(Integer pid,List<CollectionsType> collectionstypes){
        List<CollectionsType> treeList = new ArrayList<>();
        for(CollectionsType collectionstype : collectionstypes){
            if(pid == null){
                if(collectionstype.getPid() == null){//这就是第一级节点
                    treeList.add(collectionstype);
                    collectionstype.setChildren(createTree(collectionstype.getId(), collectionstypes));
                }
            }else{
                if(pid.equals(collectionstype.getPid())){
                    treeList.add(collectionstype);
                    collectionstype.setChildren(createTree(collectionstype.getId(), collectionstypes));
                }
            }
            if(CollUtil.isEmpty(collectionstype.getChildren())){
                collectionstype.setChildren(null);
            }

        }
        return treeList;
    }

//    根据ID更新用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        CollectionsType collectionstype =collectionstypeService.getById(id);
        return Result.success(collectionstype);

    }

    //删除操作:
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        collectionstypeService.deleteById(id);
        return Result.success();
    }



    /*
    最初是通过List返回所有数据,而建立Result后采用Result返回数据
    @GetMapping("/list")
    public List<CollectionsType> listCollectionsTypes(){
        return collectionstypeService.listCollectionsTypes();
    }
     */
}
